//package com.yuan.gateway.filter;
//
//
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//
//import com.yuan.gateway.utils.JwtUtils;
//
//@Component
//public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAuthenticationFilter.Config> {
//
//    public JwtAuthenticationFilter() {
//        super(Config.class);
//    }
//
//    @Override
//    public GatewayFilter apply(Config config) {
//        return (exchange, chain) -> {
//            ServerHttpRequest request = exchange.getRequest();
//
//            if (!isSecured(request)) {
//                return chain.filter(exchange);
//            }
//
//            if (!request.getHeaders().containsKey("Authorization")) {
//                ServerHttpResponse response = exchange.getResponse();
//                response.setStatusCode(HttpStatus.UNAUTHORIZED);
//                return response.setComplete();
//            }
//
//            String token = request.getHeaders().getOrEmpty("Authorization").get(0).substring(7);
//            if (!JwtUtils.validateToken(token)) {
//                ServerHttpResponse response = exchange.getResponse();
//                response.setStatusCode(HttpStatus.UNAUTHORIZED);
//                return response.setComplete();
//            }
//
//            return chain.filter(exchange);
//        };
//    }
//
//    private boolean isSecured(ServerHttpRequest request) {
//        // 这里可以配置哪些路径需要验证 Token
//        String path = request.getURI().getPath();
//        return!path.startsWith("/login");
//    }
//
//    public static class Config {
//        // 配置类，可用于传递额外的配置信息
//    }
//}
